package com.laizhenghua.order.controller;

import com.laizhenghua.commons.entity.CommonResult;
import com.laizhenghua.commons.entity.PaymentEntity;
import com.laizhenghua.commons.utils.R;
import com.laizhenghua.order.service.PaymentFeignService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description: orderController
 * @date: 2022/2/13 10:12
 */
@Slf4j
@RestController
@RequestMapping(path = "/order")
@DefaultProperties(defaultFallback = "orderServiceTimeoutAndExceptionHandler")
public class OrderController {

    @Autowired
    private PaymentFeignService paymentFeignService;

    @RequestMapping(path = "/testOk", method = RequestMethod.GET)
    public R testOk() {
        return paymentFeignService.testOk();
    }

    @RequestMapping(path = "/testError", method = RequestMethod.GET)
    /*@HystrixCommand(fallbackMethod = "orderServiceTimeoutAndExceptionHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })*/
    public R testError() {
        return paymentFeignService.testError();
    }

    @RequestMapping(path = "/getSysDate", method = RequestMethod.GET)
    @HystrixCommand
    public R getSysDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sdf.format(new Date());
        int a = 10 / 0;
        return R.ok().put("sysDate", date);
    }

    public R orderServiceTimeoutAndExceptionHandler() {
        return R.error(1000, "系统正在努力处理，请稍等...");
    }
}
